package com.raylu.realtime.utils;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Description:
 * <p>
 * Create by lucienoz on 2021/12/30.
 * Copyright © 2021 lucienoz. All rights reserved.
 */
public class RedisUtil {
    private static JedisPool jedisPool=getJedisPool();

    private static JedisPool getJedisPool(){
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(100);
        poolConfig.setMinIdle(5);
        poolConfig.setMaxIdle(5);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setMaxWaitMillis(10000);
        JedisPool jedisPool = new JedisPool(poolConfig, "hadoop130.local", 6379, 100000, "redis");
        return jedisPool;
    }

    public static Jedis getJedis(){
        return jedisPool.getResource();
    }

    public static Long deleteByKey(String key){
        Jedis jedis = getJedis();
        jedis.select(6);
        Long del = jedis.del(key);
        if (jedis!=null) {
            jedis.close();
        }
        return del;

    }

}
